package server.interfaces.impl;

import java.util.Vector;

import server.interfaces.PublisherOperations;
import server.interfaces.Suscriber;
import server.operations.Operation;

public class PublisherImpl extends Thread implements PublisherOperations{

	private Vector<Operation> operations;
	private Semaphore semaphore;
	private Suscriber suscriber;
	
	public PublisherImpl(Suscriber suscriber) {
		operations = new Vector<Operation>();
		this.suscriber = suscriber;
		semaphore = new Semaphore();
	}
	
	@Override
	public void run() {
		while(true){
			if(operations.size() == 0)
				semaphore.stop();
			else
				suscriber.publish(operations.remove(0));
		}
	}
	
	@Override
	synchronized public boolean addData(Operation operation) {
		semaphore.signal();
		return operations.add(operation);
	}

}
